{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5e03494",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 2, 2],\n",
       "       [0, 0, 0, 3, 3],\n",
       "       [0, 0, 0, 1, 1],\n",
       "       [1, 1, 1, 0, 0],\n",
       "       [2, 2, 2, 0, 0],\n",
       "       [5, 5, 5, 0, 0],\n",
       "       [1, 1, 1, 0, 0]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([[0,0,0,2,2],\n",
    "                [0,0,0,3,3],\n",
    "                [0,0,0,1,1],\n",
    "                [1,1,1,0,0],\n",
    "                [2,2,2,0,0],\n",
    "                [5,5,5,0,0],\n",
    "                [1,1,1,0,0]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "828832ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "u,sigma,v=np.linalg.svd(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "92051795",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.22044605e-16,  5.34522484e-01,  8.41650989e-01,\n",
       "         5.59998398e-02, -5.26625636e-02,  2.77555756e-17,\n",
       "         1.38777878e-17],\n",
       "       [ 0.00000000e+00,  8.01783726e-01, -4.76944344e-01,\n",
       "        -2.09235996e-01,  2.93065263e-01, -4.01696905e-17,\n",
       "        -2.77555756e-17],\n",
       "       [ 0.00000000e+00,  2.67261242e-01, -2.52468946e-01,\n",
       "         5.15708308e-01, -7.73870662e-01,  1.54770403e-16,\n",
       "         0.00000000e+00],\n",
       "       [-1.79605302e-01,  0.00000000e+00,  7.39748546e-03,\n",
       "        -3.03901436e-01, -2.04933639e-01,  8.94308074e-01,\n",
       "        -1.83156768e-01],\n",
       "       [-3.59210604e-01,  0.00000000e+00,  1.47949709e-02,\n",
       "        -6.07802873e-01, -4.09867278e-01, -4.47451355e-01,\n",
       "        -3.64856984e-01],\n",
       "       [-8.98026510e-01,  0.00000000e+00, -8.87698255e-03,\n",
       "         3.64681724e-01,  2.45920367e-01, -1.07974660e-16,\n",
       "        -1.12074131e-17],\n",
       "       [-1.79605302e-01,  0.00000000e+00,  7.39748546e-03,\n",
       "        -3.03901436e-01, -2.04933639e-01,  5.94635264e-04,\n",
       "         9.12870736e-01]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0b55421",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9.64365076e+00, 5.29150262e+00, 7.40623935e-16, 4.05103551e-16,\n",
       "       2.21838243e-32])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a683d0",
   "metadata": {},
   "source": [
    "n=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c51fe1a3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-5.77350269e-01, -5.77350269e-01, -5.77350269e-01,\n",
       "         0.00000000e+00,  0.00000000e+00],\n",
       "       [-2.46566547e-16,  1.23283273e-16,  1.23283273e-16,\n",
       "         7.07106781e-01,  7.07106781e-01],\n",
       "       [-7.83779232e-01,  5.90050124e-01,  1.93729108e-01,\n",
       "        -2.77555756e-16, -2.22044605e-16],\n",
       "       [-2.28816045e-01, -5.64364703e-01,  7.93180748e-01,\n",
       "         2.22044605e-16, -1.11022302e-16],\n",
       "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        -7.07106781e-01,  7.07106781e-01]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fce272ba",
   "metadata": {},
   "source": [
    "取k=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c7059766",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 5.38896529e-16,  1.58498979e-15,  1.58498979e-15,\n",
       "          2.00000000e+00,  2.00000000e+00],\n",
       "        [-1.04609326e-15,  5.23046632e-16,  5.23046632e-16,\n",
       "          3.00000000e+00,  3.00000000e+00],\n",
       "        [-3.48697754e-16,  1.74348877e-16,  1.74348877e-16,\n",
       "          1.00000000e+00,  1.00000000e+00],\n",
       "        [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,\n",
       "          0.00000000e+00,  0.00000000e+00],\n",
       "        [ 2.00000000e+00,  2.00000000e+00,  2.00000000e+00,\n",
       "          0.00000000e+00,  0.00000000e+00],\n",
       "        [ 5.00000000e+00,  5.00000000e+00,  5.00000000e+00,\n",
       "          0.00000000e+00,  0.00000000e+00],\n",
       "        [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,\n",
       "          0.00000000e+00,  0.00000000e+00]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_arr=np.mat(u[:,0:2])*np.mat(np.diag(sigma[0:2]))*np.mat(v[0:2,:])\n",
    "new_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b0eead85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0, 0, 4, 4],\n",
       "       [5, 5, 5, 3, 3],\n",
       "       [2, 4, 2, 1, 2],\n",
       "       [1, 1, 1, 5, 4]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food = np.array([[2,0,0,4,4],[5,5,5,3,3],[2,4,2,1,2],[1,1,1,5,4]])\n",
    "food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3e890ae8",
   "metadata": {},
   "outputs": [],
   "source": [
    "u,sigma,v=np.linalg.svd(food)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e75112be",
   "metadata": {},
   "outputs": [],
   "source": [
    "simple_food=np.mat(u[:,0:2])*np.mat(np.diag(sigma[0:2]))*np.mat(v[0:2,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5c97fce8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.3108024 , 0.23296409, 0.3373832 , 4.34636328, 3.79718712],\n",
       "        [4.62821448, 5.513132  , 4.6502802 , 2.88204388, 3.26294879],\n",
       "        [2.54169989, 3.15283808, 2.646119  , 1.2501511 , 1.52124023],\n",
       "        [1.72362839, 0.69423601, 0.72969518, 4.68444671, 4.15416774]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f15a4f2c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
